package com.config.center.database.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.wpp.constant.Constant;
import com.wpp.register.config.ZkConfigCenter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @Classname DataSourceConfig
 * @Description TODO
 * @Date 2020/12/12 22:33
 * @Created by wpp25
 */
@Configuration
public class DataSourceConfig {

    @Bean
    public ZkConfigCenter zkConfigCenter(){
        return new ZkConfigCenter();
    }

    @Bean
    public DynamicDataSource dynamicDataSource(ZkConfigCenter zkConfigCenter) {
        DruidDataSource druidDataSource = new DruidDataSource();
        // 加载 配置中心的数据源信息
        Properties properties = zkConfigCenter.loadConfig();

        // 设置数据源
        druidDataSource.setDriverClassName(properties.getProperty(Constant.DataSource.DRIVER_CLASS_NAME));
        druidDataSource.setUrl(properties.getProperty(Constant.DataSource.URL));
        druidDataSource.setUsername(properties.getProperty(Constant.DataSource.USER_NAME));
        druidDataSource.setPassword(properties.getProperty(Constant.DataSource.PASSWORD));

        return new DynamicDataSource(druidDataSource);
    }
}
